<?php

class searchController extends CController{
    
    public $model_name = LINK;
    public function AfterLoad(){
         if(!empty($_POST)){
             $add = "";
             if(!isset($_POST['q'])){
                 $add .= "&q=".$_GET['q'];
                 unset($_POST['antispam']);
                 unset($_POST['enviar']);
                 foreach($_POST as $name => $var) {
                     if(is_array($var)) $var = implode("-",$var);
                     $add .= "&$name=$var";
                 }
             }
             else $add .="&q=".$_POST['q'];
             
             $url = URL."index.php?url=".CURRENT_MODULE."/".CURRENT_CONTROLLER.$add;
             SRedirect($url);
             die();
         }
    }
    
    public function index(){
        $this->pesquisar();
        $this->display(LINK.'/index');
    } 
    
    private function pesquisar(){
        $filtro = isset($this->vars[0])?$this->vars[0]:"all";
        $q = isset($_GET['q'])?$_GET['q']:"";
        $page = isset($_GET['page'])?$_GET['page']:0;
        $page = str_replace("/", "", $page);
        $this->registerVar('palavra', $q);
        if($q != "") $result = $this->model->search($q, $_GET, $page, $filtro);
        else $result = array();
        $this->registerVar('result', $result);
        $this->model->RefineForm();
    }
}

?>
